package com.michaelwolf.mytool.wcfcompmanger.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;

import com.michaelwolf.mytool.wcfcompmanger.wcfanalyzer.WCFManager;

public class WCFAnalyzerUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JPanel controlPanel = new JPanel();
	JLabel compKeyword = new JLabel("Keyword :");
	JTextField compKeywordInput = new JTextField();
	JButton searchButton = new JButton("Search Relate Component");

	JTree relateTree = new JTree();

	WCFManager wcfManager = null;
	JLabel loadStatus = new JLabel("Loading...");
	JComboBox<String> moduleDropdown = new JComboBox<>();
	String currentSelectModule = "Search From All Module";

	public WCFAnalyzerUI() {
		super("WCF Analyzer");
		setSize(1000, 800);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		controlPanel.setLayout(null);

		compKeyword.setBounds(10, 30, 60, 30);
		controlPanel.add(compKeyword);
		compKeywordInput.setBounds(80, 30, 200, 30);
		controlPanel.add(compKeywordInput);
		searchButton.setBounds(290, 30, 200, 30);
		controlPanel.add(searchButton);

		relateTree.setBounds(10, 70, 965, 650);
		controlPanel.add(relateTree);

		loadStatus.setBounds(800, 30, 100, 30);
		controlPanel.add(loadStatus);

		moduleDropdown.setBounds(510, 30, 260, 30);
		controlPanel.add(moduleDropdown);

		add(controlPanel);
		setVisible(true);

		wcfManager = WCFManager
				.createNewWCFManager();

		searchButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String keyword = compKeywordInput.getText();
				if (keyword == null || keyword.length() == 0) {
					JOptionPane.showMessageDialog(null, "Keyword is empty",
							"Hint", JOptionPane.QUESTION_MESSAGE);

					return;
				}

				if (!wcfManager.isLoadDone()) {
					JOptionPane.showMessageDialog(null,
							"Please waiting for load done!", "Hint",
							JOptionPane.QUESTION_MESSAGE);

					return;
				}

				try {
					if (currentSelectModule.equals("Search From All Module")) {
						wcfManager.searchNode(keyword,
								wcfManager.getAllModuleName());
					} else {
						wcfManager.searchNode(keyword,
								Arrays.asList(currentSelectModule));
					}

					NodeTreeDiplayService.displayNodeOnTree(
							wcfManager.getTraverseCacheData(), relateTree, keyword);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		// relateTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		relateTree.setToggleClickCount(1);

		relateTree.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {

				if (!SwingUtilities.isLeftMouseButton(e)
						|| e.getClickCount() != 2) {
					return;
				}
				// TODO Auto-generated method stub
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) relateTree
						.getLastSelectedPathComponent();

				if (node == null)
					return;

				if (String.class.isInstance(node.getUserObject())) {
					return;
				}
				
				Object userObject = node.getUserObject();
				if(userObject instanceof WCFNodeObject){
					new NodeInfoDisplayFrame((WCFNodeObject)userObject);
				}else if (userObject instanceof FileNodeObject){
					try {
						new NodeInfoDisplayFrame((FileNodeObject)userObject);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}

				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		});

		moduleDropdown.addItem("Search From All Module");
		for (String moduleName : wcfManager.getAllModuleName()) {
			moduleDropdown.addItem(moduleName);
		}
		moduleDropdown.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (ItemEvent.SELECTED == e.getStateChange()) {
					currentSelectModule = (String) moduleDropdown
							.getSelectedItem();
				}

			}
		});

		while (!wcfManager.isLoadDone()) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		loadStatus.setText("Load Done!");

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new WCFAnalyzerUI();
	}

}
